package com.lb.jwitter.action;


import net.sf.json.JSONArray; 
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; 
import org.springframework.test.context.ContextConfiguration;
import com.lb.jwitter.util.ApplicationContextProvider; 
import net.sf.json.JSONObject; 
import net.sf.json.JSONSerializer; 
import java.util.Map; 
import com.opensymphony.xwork2.ActionContext; 
import java.text.DateFormat; 
import java.text.SimpleDateFormat; 
import java.util.ArrayList; 
import java.util.Collection; 
import java.util.Date; 
import java.util.GregorianCalendar; 
import javax.xml.datatype.DatatypeFactory; 
import javax.xml.datatype.XMLGregorianCalendar; 
import org.springframework.context.support.ClassPathXmlApplicationContext; 
import com.lb.jwitter.service.ConditionReport; 
import com.lb.jwitter.util.Message; 
import com.opensymphony.xwork2.Action; 
import com.lb.jwitter.service.TblQLDTTXADonXinGiaHanHPChiTiet;
import com.lb.jwitter.service.TblQLDTTXADonXinGiaHanHPChiTietService;
import com.lb.jwitter.service.TblQuyenChiTietService;import com.lb.jwitter.service.TblTaiKhoan;import com.lb.jwitter.service.TblQLDTTXADonXinGiaHanHocPhi;
import com.lb.jwitter.service.TblQLDTTXADonXinGiaHanHocPhiService;
import com.lb.jwitter.service.TblQLDTTXAHocVien;
import com.lb.jwitter.service.TblQLDTTXAHocVienService;

import com.lb.jwitter.service.TblCanBo;
import com.lb.jwitter.service.TblCanBoService;

import com.lb.jwitter.service.TblQLDTTXAHocKyTrongNam;
import com.lb.jwitter.service.TblQLDTTXAHocKyTrongNamService;

import com.lb.jwitter.service.TblQLDTTXAHocKyTrongNam;
import com.lb.jwitter.service.TblQLDTTXAHocKyTrongNamService;

import com.lb.jwitter.service.TblQLDTTXAHocVienHocPhiMonHoc;

import com.lb.jwitter.service.TblQLDTTXAHocVienHocPhiMonHocService;

import com.lb.jwitter.service.TblQLDTTXAHocVienHocPhiService;

import com.lb.jwitter.service.TblQLDTTXAMonHocService;

 /** 
 * @author QUOCVIET  
 * 
 */ 


@ContextConfiguration(locations="classpath:beans.xml") 
public class TblQLDTTXADonXinGiaHanHocPhiInsertAction{
    private int pageSize; 
    public int getPageSize() {
       return pageSize;
 }

    public void setPageSize(int pageSize) { this.pageSize = pageSize; }

    private int page; 
    public int getPage() { return page; }

    public void setPage(int page) { this.page = page; }

    private int pages; 
    public int getPages() { return pages; }

    public void setPages(int pages) { this.pages = pages; }

    private String dataString; 
    public String getDataString() { return dataString; }

    private Message msg; 
    public Message getMsg() { return msg; }

    private Long id; 
    public void setId(Long id) {
       this.id = id;
 }

    private int hocVienId; 
    public void setHocVienId(int hocVienId) {
       this.hocVienId = hocVienId;
 }

    public int getHocVienId() {
       return hocVienId;
 }

    private Collection<TblQLDTTXAHocVien> tblQLDTTXAHocViens; 
    public Collection<TblQLDTTXAHocVien>  getTblQLDTTXAHocViens(){ 
              return tblQLDTTXAHocViens; 
     } 

    private TblQLDTTXAHocVienService tblQLDTTXAHocVienService; 

    private String ngayNop; 
    public void setNgayNop(String ngayNop) {
       this.ngayNop = ngayNop;
 }

    public String getNgayNop() {
       return ngayNop;
 }

    private int canBoId; 
    public void setCanBoId(int canBoId) {
       this.canBoId = canBoId;
 }

    public int getCanBoId() {
       return canBoId;
 }

    private Collection<TblCanBo> tblCanBos; 
    public Collection<TblCanBo>  getTblCanBos(){ 
              return tblCanBos; 
     } 

    private TblCanBoService tblCanBoService; 

    private int tuHocKyId; 
    public void setTuHocKyId(int tuHocKyId) {
       this.tuHocKyId = tuHocKyId;
 }

    public int getTuHocKyId() {
       return tuHocKyId;
 }



    private int sangHocKyId; 
    public void setSangHocKyId(int sangHocKyId) {
       this.sangHocKyId = sangHocKyId;
 }

    public int getSangHocKyId() {
       return sangHocKyId;
 }

    private Collection<TblQLDTTXAHocKyTrongNam> tblQLDTTXAHocKyTrongNams; 
    public Collection<TblQLDTTXAHocKyTrongNam>  getTblQLDTTXAHocKyTrongNams(){ 
              return tblQLDTTXAHocKyTrongNams; 
     } 

    private TblQLDTTXAHocKyTrongNamService tblQLDTTXAHocKyTrongNamService; 

    private double tongTienGiaHan; 
    public void setTongTienGiaHan(double tongTienGiaHan) {
       this.tongTienGiaHan = tongTienGiaHan;
 }

    public double getTongTienGiaHan() {
       return tongTienGiaHan;
 }

    private String lyDo; 
    public void setLyDo(String lyDo) {
       this.lyDo = lyDo;
 }

    public String getLyDo() {
       return lyDo;
 }

    private int trangThai; 
    public void setTrangThai(int trangThai) {
       this.trangThai = trangThai;
 }

    public int getTrangThai() {
       return trangThai;
 }

    private TblQLDTTXADonXinGiaHanHocPhi tblQLDTTXADonXinGiaHanHocPhi; 
    public TblQLDTTXADonXinGiaHanHocPhi  getTblQLDTTXADonXinGiaHanHocPhi() {
        return tblQLDTTXADonXinGiaHanHocPhi;
 }

    private Collection<TblQLDTTXADonXinGiaHanHocPhi> tblQLDTTXADonXinGiaHanHocPhis; 
    public Collection<TblQLDTTXADonXinGiaHanHocPhi>  getTblQLDTTXADonXinGiaHanHocPhis() {
         return tblQLDTTXADonXinGiaHanHocPhis; 
      } 

    private TblQLDTTXADonXinGiaHanHocPhiService tblQLDTTXADonXinGiaHanHocPhiService; 

    private TblQLDTTXADonXinGiaHanHPChiTietService tblQLDTTXADonXinGiaHanHPChiTietService; 
    
    private String tblQLDTTXAHocVienHocPhiMonHocStr; 
    public void setTblQLDTTXAHocVienHocPhiMonHocStr(String tblQLDTTXAHocVienHocPhiMonHocStr) 
{
       this.tblQLDTTXAHocVienHocPhiMonHocStr = tblQLDTTXAHocVienHocPhiMonHocStr;
 }

    public String getTblQLDTTXAHocVienHocPhiMonHocStr() 
 {
       return tblQLDTTXAHocVienHocPhiMonHocStr;
 }

    private TblQLDTTXAHocVienHocPhiMonHocService tblQLDTTXAHocVienHocPhiMonHocService; 

    private TblQLDTTXAHocVienHocPhiService tblQLDTTXAHocVienHocPhiService; 

    private TblQLDTTXAMonHocService tblQLDTTXAMonHocService; 

	private int preInsert(Message msg, TblQLDTTXADonXinGiaHanHocPhi obj ){ 
        msg.setPreMessage_b(true); 
        msg.setPreMessage_s("success"); 
        return 1; 
    } 
	private int postInsert(Message msg, TblQLDTTXADonXinGiaHanHocPhi obj ){ 
        msg.setPostMessage_b(true); 
        msg.setPostMessage_s("success"); 
        return 1; 
    } 
  private TblTaiKhoan taiKhoanDangNhap; 
 public TblTaiKhoan getTaiKhoanDangNhap() { 
     return taiKhoanDangNhap; 
 } 
 public void setTaiKhoanDangNhap(TblTaiKhoan taiKhoanDangNhap) { 
     this.taiKhoanDangNhap = taiKhoanDangNhap; 
 } 
 private TblQuyenChiTietService tblQuyenChiTietService;
	 @Autowired // Injected by Spring 
     ApplicationContextProvider contextProvider; 
 public String execute() throws Exception 
{ 
 try{ 	
     Map session = ActionContext.getContext().getSession();
     taiKhoanDangNhap =  (TblTaiKhoan) session.get("taiKhoan");
     if( taiKhoanDangNhap == null) return "noprivilege";
    ApplicationContext context = ApplicationContextProvider.getApplicationContext(); 
    tblQuyenChiTietService = (TblQuyenChiTietService) context.getBean("tbl_Quyen_ChiTietClient"); 
    if(!tblQuyenChiTietService.checkRight(taiKhoanDangNhap.getMaNhomQuyen().getId(), "tbl_QLDTTXA_DonXinGiaHanHocPhi", "them")){ 
        return "noprivilege"; 
    }        
    
    tblQLDTTXADonXinGiaHanHocPhiService = (TblQLDTTXADonXinGiaHanHocPhiService) context.getBean("tbl_QLDTTXA_DonXinGiaHanHocPhiClient"); 
    tblQLDTTXADonXinGiaHanHPChiTietService = (TblQLDTTXADonXinGiaHanHPChiTietService) context.getBean("tbl_QLDTTXA_DonXinGiaHanHP_ChiTietClient"); 
        tblQLDTTXAHocVienService = (TblQLDTTXAHocVienService) context.getBean("tbl_QLDTTXA_HocVienClient"); 

        tblCanBoService = (TblCanBoService) context.getBean("tbl_CanBoClient"); 

     
        tblQLDTTXAHocKyTrongNamService = (TblQLDTTXAHocKyTrongNamService) context.getBean("tbl_QLDTTXA_HocKyTrongNamClient"); 

        tblQLDTTXAHocVienHocPhiMonHocService = (TblQLDTTXAHocVienHocPhiMonHocService) context.getBean("tbl_QLDTTXA_HocVienHocPhi_MonHocClient"); 

        tblQLDTTXAHocVienHocPhiService = (TblQLDTTXAHocVienHocPhiService) context.getBean("tbl_QLDTTXA_HocVien_HocPhiClient"); 

        tblQLDTTXAMonHocService = (TblQLDTTXAMonHocService) context.getBean("tbl_QLDTTXA_MonHocClient"); 

        msg = new Message(); 
        tblQLDTTXADonXinGiaHanHocPhi = new TblQLDTTXADonXinGiaHanHocPhi(); 
        tblQLDTTXADonXinGiaHanHocPhi.setHocVienId(tblQLDTTXAHocVienService.gettblQLDTTXAHocVien(new Long(hocVienId))); 
       DateFormat df = new SimpleDateFormat("dd/MM/yyyy"); 
       Date dd = df.parse(ngayNop); 
       GregorianCalendar c = new GregorianCalendar(); 
       c.setTime(dd);
       XMLGregorianCalendar d = DatatypeFactory.newInstance().newXMLGregorianCalendar(c); 
        tblQLDTTXADonXinGiaHanHocPhi.setNgayNop(d); 
        tblQLDTTXADonXinGiaHanHocPhi.setCanBoId(tblCanBoService.gettblCanBo(new Long(canBoId))); 
        tblQLDTTXADonXinGiaHanHocPhi.setTuHocKyId(tblQLDTTXAHocKyTrongNamService.gettblQLDTTXAHocKyTrongNam(new Long(tuHocKyId))); 
        tblQLDTTXADonXinGiaHanHocPhi.setSangHocKyId(tblQLDTTXAHocKyTrongNamService.gettblQLDTTXAHocKyTrongNam(new Long(sangHocKyId))); 
        tblQLDTTXADonXinGiaHanHocPhi.setTongTienGiaHan(tongTienGiaHan); 
        tblQLDTTXADonXinGiaHanHocPhi.setLyDo(lyDo); 
        if(trangThai == 1){ 
           tblQLDTTXADonXinGiaHanHocPhi.setTrangThai(true); 
       } else { 
           tblQLDTTXADonXinGiaHanHocPhi.setTrangThai(false); 
       } 
        preInsert(msg,tblQLDTTXADonXinGiaHanHocPhi); 
       if (msg.isPreMessage_b()) 
       { 
           id = tblQLDTTXADonXinGiaHanHocPhiService.createtblQLDTTXADonXinGiaHanHocPhi(tblQLDTTXADonXinGiaHanHocPhi); 
           postInsert(msg, tblQLDTTXADonXinGiaHanHocPhi); 
           if (msg.isPostMessage_b() == false) 
           { 
               return Action.ERROR; 
           } 
       }else{ 
           return Action.ERROR; 
       } 
       tblQLDTTXADonXinGiaHanHocPhi.setId(id);
       
       
	JSONArray jsonTblQLDTTXAHocVienHocPhiMonHoc = (JSONArray)JSONSerializer.toJSON(tblQLDTTXAHocVienHocPhiMonHocStr); 
	for (int i = 0; i < jsonTblQLDTTXAHocVienHocPhiMonHoc.size(); i++) 
	 { 
	      JSONObject jsObj = jsonTblQLDTTXAHocVienHocPhiMonHoc.getJSONObject(i); 
	        TblQLDTTXAHocVienHocPhiMonHoc tblQLDTTXAHocVienHocPhiMonHoc = new TblQLDTTXAHocVienHocPhiMonHoc();
	        tblQLDTTXAHocVienHocPhiMonHoc.setId(jsObj.getLong("hocVienHocPhiMonHocId")); 
	      TblQLDTTXADonXinGiaHanHPChiTiet donXinGiaHanHPChiTiet = new TblQLDTTXADonXinGiaHanHPChiTiet();
	      donXinGiaHanHPChiTiet.setDonGiaHanId(tblQLDTTXADonXinGiaHanHocPhi);
	     donXinGiaHanHPChiTiet.setHocVienHocPhiMonHocId(tblQLDTTXAHocVienHocPhiMonHoc);
	     donXinGiaHanHPChiTiet.setDaXet(false);
	     
	     tblQLDTTXADonXinGiaHanHPChiTietService.createtblQLDTTXADonXinGiaHanHPChiTiet(donXinGiaHanHPChiTiet);
	      
	   } 
        return Action.SUCCESS; 
      } 
      catch(Exception e){ 
         System.out.println("Loi Insert tbl_QLDTTXA_DonXinGiaHanHocPhi Action"); 
         e.printStackTrace(); 
         return Action.ERROR; 
 } 
 } 
 } 
 
